<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends CommntController {
	public function index()
	{	
		$article = M('article');
		$atic = $article->where(array('erase'=>0))->field('id,content,title,status,add_time,update_time,img,iselite')->order('id desc')->select();
		// var_dump($atic);die;
		$this->assign('list',$atic);
		$this->display();
	}
	/**
	 * 添加
	 */
	public function add()
	{
		$this->display();	
	}
	/**
	 * 添加+图片处理
	 */
	public  function upload()
	{
		// var_dump($_FILES);die;
		//判断是否是POST传过来的值
        if (!IS_POST) {
            echo "1";die;
        }
        //获取传过来的图片进行处理
        $file     = $_FILES['imgs'];
        $data     = $_POST;
        $data['add_time'] = date('Y-m-d H:i:s', time());
        $data['update_time'] = date('Y-m-d H:i:s', time());

        $filetype = substr(strrchr($file['name'],'.'),1);
        $time 	  = time('YmdHis').rand(1,20);
        $imgtype  = $time.'.'.$filetype;
        //判断file里是否有值并且判断图片名是否获取成功
        if (isset($file) && $file['name'] != '') {
        	//判断上传文件是否是array里的类型
           	$arrayName = array("image/jpg","image/png","image/jpeg","image/gif");
           	$fiel_type = $file['type'];
           	        // var_dump(in_array($fiel_type,$arrayName));die;
           	if (!in_array($fiel_type,$arrayName)) {
           		echo "2";die;
           	}
        }
        //图片存放路径
        $path   = "./Public/article/".$imgtype;
        //判断图片是从哪个位置传过来的,是否通过HTTP POST 传递的
        if (is_uploaded_file($file['tmp_name'])) {
        	if (move_uploaded_file($file['tmp_name'],$path)) {
        		$image = new \Think\Image();//图片处理类
                $image->open($path);
                // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
                $image->thumb(229, 195,\Think\Image::IMAGE_THUMB_SCALE)->save($path);
                clearstatcache();//清除缓存
        		if(filesize($path)>(50*1024))
                {
                    $image->thumb(400,400,\Think\Image::IMAGE_THUMB_SCALE)->save($path);
                }
                $data['img'] = $imgtype;
        	}
        }
        $article      = M('article');
       if ($article->add($data)) {
       		//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
            // $this->success('添加成功', U('Article/index'));
            $url = U('Article/index');
            echo "3#".$url;die;
       }else{
       		//错误页面的默认跳转页面是返回前一页，通常不需要设置
            // $this->error('添加失败',U('Article/add'));
            $url = U('Article/add');
            echo "4#".$url;die;
       }   
	}

	/**
	 * 修改
	 */
	public function edit()
	{
		$id = $_GET['id'];
		$moder = M('article')->where(array('id'=>$id))->find();
		$this->assign('list',$moder);
		$this->display();
	}
	/**
	 * 更新修改后的文章
	 */
	public function article_edit()
	{
		  //判断是否是POST传过来的值
        if (!IS_POST) {
            echo "1";die;
        }
        // var_dump($_FILES);die;
        //获取传过来的图片进行处理
        $file     = $_FILES['imgs'];
        // var_dump($file);die;
        $data     = $_POST;
        $data['add_time'] = date('Y-m-d H:i:s', time());
        $data['update_time'] = date('Y-m-d H:i:s', time());
        $filetype = substr(strrchr($file['name'],'.'),1);
        $time 	  = time('YmdHis').rand(1,20);
        $imgtype  = $time.'.'.$filetype;
        //判断file里是否有值并且判断图片名是否获取成功
        if (isset($file) && $file['name'] != '') {
        	//判断上传文件是否是array里的类型
           	$arrayName = array("image/jpg","image/png","image/jpeg","image/gif");
           	$fiel_type = $file['type'];
           	        // var_dump(in_array($fiel_type,$arrayName));die;
           	if (!in_array($fiel_type,$arrayName)) {
           		echo "2";die;
           	}
        }
        
         //图片存放路径
        $path   = "./Public/article/".$imgtype;

        //判断图片是从哪个位置传过来的,是否通过HTTP POST 传递的
        if (is_uploaded_file($file['tmp_name'])) {
          	if (move_uploaded_file($file['tmp_name'],$path)) {
          		$image = new \Think\Image();//图片处理类
                  $image->open($path);
                  // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg 14879166587.jpg
                  $image->thumb(229, 195,\Think\Image::IMAGE_THUMB_SCALE)->save($path);
                  clearstatcache();//清除缓存
          		if(filesize($path)>(50*1024))
                  {
                      $image->thumb(400,400,\Think\Image::IMAGE_THUMB_SCALE)->save($path);
                  }
                  $data['img'] = $imgtype;
                  // echo $data['img'];
          	}
        }
        $article      = M('article');
        $username = $article->where(array('id'=>$data['id']))->find();	
        if (!$file['img'] == '') {
       
             if ($username['img'] == null && $username['img'] =='') {
             	
             }else{

             		$uploadDir = './Public/article/'.($username['img']);
             		
  	            // 判断是id对应的字段否存在
  	            if ($username['img']) {
  	                unlink('./Public/article/'.$username['img']);
  	            }

             }
          }     
            $list = $article->where(array('id'=>$data['id']))->save($data);
	        if ( $list ) {
	            //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
	            // $this->success('修改成功', U('User/index'));
	            $url = U('Article/index');
	            echo "4#".$url;die;
	        }else{
	            //错误页面的默认跳转页面是返回前一页，通常不需要设置
	            // $this->error('修改失败');
	            // $url = U('User/user_edit');
	            $url = U('Article/edit');
	            echo "5#".$url;die;
	        } 
	}

	/**
	 * 回收站
	 */
	public  function  delete($id = '')
	{
  		$id=$_GET['id'];
  		$moder = M('article');
  		$list  = $moder->where(array('id'=>$id))->find();
  		if ($list['erase'] == 0) {
  			 $list['erase'] = 1;
              $moder->save($list);
              echo "1";die;
  		}else {
  			echo "2";die;
  		}
  }

  /**
  * 推荐
  */
  public  function  choose($id = '')
  {
      $id=$_GET['id'];
      $moder = M('article');
      $list  = $moder->where(array('id'=>$id))->find();
      if ($list['iselite'] == 0) {
              $list['iselite'] = 1;
              $moder->save($list);
              echo "1";die;
          }else if ($list['iselite'] == 1) {
            $list['iselite'] = 0;
              $moder->save($list);
              echo "2";die;
          } 
  }
  /**
   * 查看详情
   */
  public function Article_list()
  {
      $id=$_GET['id'];
      $moder = M('article');
      $list  = $moder->where(array('id'=>$id))->find();
      
      $this->assign('moder',$list);
      $this->display();
  }
	
}